home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / JLayeredPane.java < prev    next >
Text File  |  1998-06-30  |  26KB  |  705 lines

  1. /*
  2.  * @(#)JLayeredPane.java    1.25 98/03/12
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20. package com.sun.java.swing;
  21.  
  22. import java.awt.Component;
  23. import java.util.Hashtable;
  24. import java.awt.Color;
  25. import java.awt.Graphics;
  26. import java.awt.Rectangle;
  27.  
  28. import com.sun.java.accessibility.*;
  29.  
  30. /**
  31.  * JLayeredPane adds depth to a JFC/Swing container, allowing components to overlap 
  32.  * each other when needed. An Integer object specifies each component's depth in the
  33.  * container, where higher-numbered components sit "on top" of other 
  34.  * components.
  35.  * <P>
  36.  * <TABLE ALIGN="RIGHT" BORDER="0">
  37.  * <TR>
  38.  *   <TD ALIGN="CENTER">
  39.  *     <P ALIGN="CENTER"><IMG SRC="doc-files/JLayeredPane-1.gif" WIDTH="269" HEIGHT="264" ALIGN="BOTTOM" BORDER="0">
  40.  *   </TD>
  41.  * </TR>
  42.  * </TABLE>
  43.  * For convenience, JLayeredPane divides the depth-range into several different 
  44.  * layers. Putting a component into one of those layers makes it easy to ensure 
  45.  * that components overlap properly, without having to worry about specifying
  46.  * numbers for specific depths:
  47.  * <DL>
  48.  *    <DT><FONT SIZE="2">DEFAULT_LAYER</FONT></DT>
  49.  *         <DD>The standard layer, where most components go. This the bottommost 
  50.  *         layer.
  51.  *    <DT><FONT SIZE="2">PALETTE_LAYER</FONT></DT>
  52.  *         <DD>The palette layer sits over the default layer. Useful for floating 
  53.  *         toolbars and palettes, so they can be positioned above other components.
  54.  *    <DT><FONT SIZE="2">MODAL_LAYER</FONT></DT>
  55.  *         <DD>The layer used for modal dialogs. They will appear on top of any
  56.  *         toolbars, palettes, or standard components in the container.
  57.  *    <DT><FONT SIZE="2">POPUP_LAYER</FONT></DT>
  58.  *         <DD>The popup layer displays above dialogs. That way, the popup windows 
  59.  *         associated with combo boxes, tooltips, and other help text will appear 
  60.  *         above the component, palette, or dialog that generated them.
  61.  *    <DT><FONT SIZE="2">DRAG_LAYER</FONT></DT>
  62.  *         <DD>When dragging a component, reassigning it to the drag layer ensures 
  63.  *         that it is positioned over every other component in the container. When 
  64.  *         finished dragging, it can be reassigned to its normal layer.
  65.  * </DL>
  66.  * The JLayeredPane methods <code>moveToFront(Component)</code>, 
  67.  * <code>moveToBack(Component)</code> and <code>setPosition</code> can be used 
  68.  * to reposition a component within its layer. The <code>setLayer</code> method 
  69.  * can also be used to change the component's current layer.
  70.  *
  71.  * <h2>Details</h2>
  72.  * JLayeredPane manages it's list of children like Container, but
  73.  * allows for the definition of a several layers within itself. Children
  74.  * in the same layer are managed exactly like the normal Container object,
  75.  * with the added feature that when children components overlap, children 
  76.  * in higher layers display above the children in lower layers.
  77.  * <p>  
  78.  * Each layer is a distinct integer number. The layer attribute can be set 
  79.  * on a Component by passing an Integer object during the add call.<br>
  80.  * For example:
  81.  * <PRE>
  82.  *     layeredPane.add(child, JLayeredPane.DEFAULT_LAYER);
  83.  * or
  84.  *     layeredPane.add(child, new Integer(10));
  85.  * </PRE>
  86.  * The layer attribute can also be set on a Component by calling<PRE>
  87.  *     layeredPaneParent.setLayer(child, 10)</PRE>
  88.  * on the JLayeredPane that is the parent of component. The layer
  89.  * should be set <i>before</i> adding the child to the parent.
  90.  * <p>
  91.  * Higher number layers display above lower number layers. So, using 
  92.  * numbers for the layers and letters for individual components, a
  93.  * representative list order would look like this:<PRE>
  94.  *       5a, 5b, 5c, 2a, 2b, 2c, 1a </PRE>
  95.  * where the leftmost components are closest to the top of the display.
  96.  * <p>
  97.  * A component can be moved to the top or bottom position within its
  98.  * layer by calling <code>moveToFront</code> or <code>moveToBack</code>.
  99.  * <p>
  100.  * The position of a component within a layer can also be specified directly.
  101.  * Valid positions range from 0 up to one less than the number of 
  102.  * components in that layer. A value of -1 indicates the bottommost
  103.  * position. A value of 0 indicates the topmost position. Unlike layer
  104.  * numbers, higher position values are <i>lower</i> in the display.
  105.  * <blockquote> 
  106.  * <b>Note:</b> This sequence (defined by java.awt.Container) is the reverse 
  107.  * of the layer numbering sequence. Usually though, you will use <code>moveToFront</code>,
  108.  * <code>moveToBack</code>, and <code>setLayer</code>.
  109.  * </blockquote>
  110.  * Here are some examples using the method add(Component, layer, position):
  111.  * Calling add(5x, 5, -1) results in:<PRE>
  112.  *       5a, 5b, 5c, 5x, 2a, 2b, 2c, 1a </PRE>
  113.  *
  114.  * Calling add(5z, 5, 2) results in:<PRE>
  115.  *       5a, 5b, 5z, 5c, 5x, 2a, 2b, 2c, 1a </PRE>
  116.  *
  117.  * Calling add(3a, 3, 7) results in:<PRE>
  118.  *       5a, 5b, 5z, 5c, 5x, 3a, 2a, 2b, 2c, 1a </PRE>
  119.  *
  120.  * Using normal paint/event mechanics results in 1a appearing at the bottom
  121.  * and 5a being above all other components.
  122.  * <p>
  123.  * <b>Note:</b> that these layers are simply a logical construct and LayoutManagers
  124.  * will affect all child components of this container without regard for
  125.  * layer settings.
  126.  * <p>
  127.  * Warning: serialized objects of this class will not be compatible with
  128.  * future swing releases.  The current serialization support is appropriate 
  129.  * for short term storage or RMI between Swing1.0 applications.  It will
  130.  * not be possible to load serialized Swing1.0 objects with future releases
  131.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  132.  * baseline for the serialized form of Swing objects.
  133.  * 
  134.  * @version 1.25 03/12/98
  135.  * @author David Kloba
  136.  * @see <a href="doc-files/FramePaneOverview.html#JLayeredPane">
  137.  *      Understanding the JFC/Swing Container Hierarchy: JLayeredPane</a>
  138.  */
  139. public class JLayeredPane extends JComponent implements Accessible {
  140.     /// Watch the values in getObjectForLayer()
  141.     /** Convenience object defining the Default layer. Equivalent to new Integer(0).*/
  142.     public final static Integer DEFAULT_LAYER = new Integer(0);
  143.     /** Convenience object defining the Palette layer. Equivalent to new Integer(100).*/
  144.     public final static Integer PALETTE_LAYER = new Integer(100);
  145.     /** Convenience object defining the Modal layer. Equivalent to new Integer(200).*/
  146.     public final static Integer MODAL_LAYER = new Integer(200);
  147.     /** Convenience object defining the Popup layer. Equivalent to new Integer(300).*/
  148.     public final static Integer POPUP_LAYER = new Integer(300);
  149.     /** Convenience object defining the Drag layer. Equivalent to new Integer(400).*/
  150.     public final static Integer DRAG_LAYER = new Integer(400);
  151.     /** Convenience object defining the Frame Content layer. 
  152.       * This layer is normally only use to positon the contentPane and menuBar 
  153.       * components of JFrame.
  154.       * Equivalent to new Integer(-30000).
  155.       * @see JFrame
  156.       */
  157.     public final static Integer FRAME_CONTENT_LAYER = new Integer(-30000);
  158.  
  159.     /** Bound property */
  160.     public final static String LAYER_PROPERTY = "layeredContainerLayer";
  161.     // Hashtable to store layer values for non-JComponent components
  162.     private Hashtable componentToLayer;
  163.     private boolean optimizedDrawingPossible = true;
  164.  
  165.  
  166. //////////////////////////////////////////////////////////////////////////////
  167. //// Container Override methods 
  168. //////////////////////////////////////////////////////////////////////////////
  169.     /** Create a new JLayeredPane */
  170.     public JLayeredPane() {
  171.         setLayout(null);
  172.     }
  173.  
  174.     private void validateOptimizedDrawing() {
  175.         boolean layeredComponentFound = false;
  176.         synchronized(getTreeLock()) {
  177.             int i,d;
  178.             Integer layer = null;
  179.  
  180.             for(i=0,d=getComponentCount();i<d;i++) {
  181.                 layer = null;
  182.                 if(getComponent(i) instanceof JInternalFrame ||
  183.                    (getComponent(i) instanceof JComponent &&
  184.                     (layer = (Integer)((JComponent)getComponent(i)).getClientProperty(LAYER_PROPERTY)) != null)) {
  185.                     if(layer != null && layer.equals(FRAME_CONTENT_LAYER))
  186.                         continue;
  187.                     layeredComponentFound = true;
  188.                     break;
  189.                 }
  190.             }
  191.         }
  192.         
  193.         if(layeredComponentFound)
  194.             optimizedDrawingPossible = false;
  195.         else
  196.             optimizedDrawingPossible = true;
  197.     }
  198.     
  199.     protected void addImpl(Component comp, Object constraints, int index) {
  200.         int layer = DEFAULT_LAYER.intValue();
  201.         int pos;
  202.  
  203.         if(constraints instanceof Integer) {
  204.             layer = ((Integer)constraints).intValue();
  205.             setLayer(comp, layer);
  206.         } else
  207.             layer = getLayer(comp);
  208.  
  209.         pos = insertIndexForLayer(layer, index);
  210.         super.addImpl(comp, constraints, pos);
  211.         comp.validate();
  212.         comp.repaint();
  213.         validateOptimizedDrawing();
  214.     }
  215.  
  216.     /**
  217.      * Remove the indexed component from this pane.
  218.      * This is the absolute index, ignoring layers. 
  219.      *
  220.      * @param index  an int specifying the component to remove
  221.      * @see #getIndexOf
  222.      */
  223.     public void remove(int index) {
  224.         Component c = getComponent(index);
  225.         super.remove(index);
  226.         validateOptimizedDrawing();
  227.     }
  228.  
  229.     /**
  230.      * Returns false if components in the pane can overlap, which makes
  231.      * optimized drawing impossible. Otherwise, returns true.
  232.      *
  233.      * @return false if components can overlap, else true
  234.      * @see JComponent#isOptimizedDrawingEnabled
  235.      */
  236.     public boolean isOptimizedDrawingEnabled() {
  237.         return optimizedDrawingPossible;
  238.     }
  239.  
  240.  
  241. //////////////////////////////////////////////////////////////////////////////
  242. //// New methods for managing layers
  243. //////////////////////////////////////////////////////////////////////////////
  244.     /** Sets the layer property on a JComponent. This method does not cause
  245.       * any side effects like setLayer() (painting, add/remove, etc).
  246.       * Normally you should use the instance method setLayer(), in order to
  247.       * get the desired side-effects (like repainting).
  248.       * 
  249.       * @param c      the JComponent to move
  250.       * @param layer  an int specifying the layer to move it to
  251.       * @see #setLayer
  252.       */
  253.     public static void putLayer(JComponent c, int layer) {
  254.         /// MAKE SURE THIS AND setLayer(Component c, int layer, int position)  are SYNCED
  255.         Integer layerObj;
  256.  
  257.         layerObj = new Integer(layer);
  258.         c.putClientProperty(LAYER_PROPERTY, layerObj);
  259.     }
  260.  
  261.     /** Gets the layer property for a JComponent, it
  262.       * does not cause any side effects like setLayer(). (painting, add/remove, etc)
  263.       * Normally you should use the instance method getLayer().
  264.       * 
  265.       * @param c  the JComponent to check
  266.       * @return   an int specifying the component's layer
  267.       */
  268.     public static int getLayer(JComponent c) {
  269.         Integer i;
  270.         if((i = (Integer)c.getClientProperty(LAYER_PROPERTY)) != null)
  271.             return i.intValue();
  272.         return DEFAULT_LAYER.intValue();
  273.     }
  274.  
  275.     /** Convenience method that returns the first JLayeredPane which
  276.       * contains the specified component. Note that all JFrames have a 
  277.       * JLayeredPane at their root, so any component in a JFrame will
  278.       * have a JLayeredPane parent.
  279.       *
  280.       * @param c the Component to check
  281.       * @return the JLayeredPane that contains the component, or
  282.       *         null if no JLayeredPane is found in the component
  283.       *         hierarchy
  284.       * @see JFrame
  285.       * @see JRootPane
  286.       */
  287.     public static JLayeredPane getLayeredPaneAbove(Component c) {
  288.         if(c == null) return null;
  289.         
  290.         Component parent = c.getParent();
  291.         while(parent != null && !(parent instanceof JLayeredPane))
  292.             parent = parent.getParent();
  293.         return (JLayeredPane)parent;
  294.     }
  295.  
  296.     /** Sets the layer attribute on the specified component,
  297.       * making it the bottommost component in that layer.
  298.       * Should be called before adding to parent. 
  299.       * 
  300.       * @param c     the Component to set the layer for
  301.       * @param layer an int specifying the layer to set, where 
  302.       *              lower numbers are closer to the bottom
  303.       */
  304.     public void setLayer(Component c, int layer)  {
  305.         setLayer(c, layer, -1);
  306.     }
  307.  
  308.     /** Sets the layer attribute for the specified component and
  309.       * also sets its position within that layer.
  310.       * 
  311.       * @param c         the Component to set the layer for
  312.       * @param layer     an int specifying the layer to set, where 
  313.       *                  lower numbers are closer to the bottom
  314.       * @param position  an int specifying the position within the
  315.       *                  layer, where 0 is the topmost position and -1
  316.       *                  is the bottommost position
  317.       */
  318.     public void setLayer(Component c, int layer, int position)  {
  319.         Integer layerObj;
  320.         layerObj = getObjectForLayer(layer);
  321.  
  322.         if(layer == getLayer(c) && position == getPosition(c)) {
  323.             if(c instanceof JComponent)
  324.                 repaint(((JComponent)c)._bounds);
  325.             else
  326.                 repaint(c.getBounds());
  327.             return;
  328.         }
  329.         
  330.         /// MAKE SURE THIS AND putLayer(JComponent c, int layer) are SYNCED
  331.         if(c instanceof JComponent)
  332.             ((JComponent)c).putClientProperty(LAYER_PROPERTY, layerObj);
  333.         else
  334.             getComponentToLayer().put(LAYER_PROPERTY, layerObj);
  335.         
  336.         if(c.getParent() == null || c.getParent() != this)      {
  337.             if(c instanceof JComponent)
  338.                 repaint(((JComponent)c)._bounds);
  339.             else
  340.                 repaint(c.getBounds());
  341.             return;
  342.         }
  343.  
  344.         // Remove the Component and re-add after re-setting the layer
  345.         // this is necessary now because I have no access to the
  346.         // components[] in Container, to reorder things.
  347.         remove(c);
  348.  
  349.         // ALERT passing NULL here for the constraints may be bad
  350.         // the current hacks to fix this smell bad right now. 
  351.         // Cannot override 
  352.         add(c, null, position);
  353.         if(c instanceof JComponent)
  354.             repaint(((JComponent)c)._bounds);
  355.         else
  356.             repaint(c.getBounds());
  357.     }
  358.  
  359.     /** 
  360.      * Returns the layer attribute for the specified Component.
  361.      * 
  362.      * @param c  the Component to check
  363.      * @return an int specifying the component's current layer
  364.      */
  365.     public int getLayer(Component c) {
  366.         Integer i;
  367.         if(c instanceof JComponent)
  368.             i = (Integer)((JComponent)c).getClientProperty(LAYER_PROPERTY);
  369.         else
  370.             i = (Integer)getComponentToLayer().get(LAYER_PROPERTY);
  371.  
  372.         if(i == null)
  373.             return DEFAULT_LAYER.intValue();
  374.         return i.intValue();
  375.     }
  376.  
  377.     /** 
  378.      * Returns the index of the specified Component. 
  379.      * This is the absolute index, ignoring layers.
  380.      * Index numbers, like position numbers, have the topmost component
  381.      * at index zero. Larger numbers are closer to the bottom.
  382.      * 
  383.      * @param c  the Component to check
  384.      * @return an int specifying the component's index 
  385.      */
  386.     public int getIndexOf(Component c) {
  387.         int i, count;
  388.         
  389.         count = getComponentCount();    
  390.         for(i = 0; i < count; i++) {
  391.             if(c == getComponent(i))
  392.                 return i;
  393.         }
  394.         return -1;
  395.     }
  396.     /** 
  397.      * Moves the component to the top of the components in it's current layer
  398.      * (position 0).
  399.      *
  400.      * @param c the Component to move 
  401.      * @see #setPosition(Component, int) 
  402.      */
  403.     public void moveToFront(Component c) {
  404.         setPosition(c, 0);
  405.     }
  406.  
  407.     /** 
  408.      * Moves the component to the bottom of the components in it's current layer
  409.      * (position -1).
  410.      *
  411.      * @param c the Component to move 
  412.      * @see #setPosition(Component, int) 
  413.      */
  414.     public void moveToBack(Component c) {
  415.         setPosition(c, getComponentCountInLayer(getLayer(c)));
  416.     }
  417.  
  418.     /**
  419.      * Moves the component to <code>position</code> within it's current layer,
  420.      * where 0 is the topmost position within the layer and -1 is the bottommost
  421.      * position. 
  422.      * <p>
  423.      * <b>Note:</b> Position numbering is defined by java.awt.Container, and
  424.      * is the opposite of layer numbering. Lower position numbers are closer
  425.      * to the top (0 is topmost), and higher position numbers are closer to
  426.      * the bottom.
  427.      *
  428.      * @param c         the Component to move
  429.      * @param position  an int in the range -1..N-1, where N is the number of
  430.      *                  components in the component's current layer
  431.      */
  432.     public void setPosition(Component c, int position) {
  433.         setLayer(c, getLayer(c), position);
  434.     }
  435.  
  436.     /**
  437.      * Get the relative position of the component within its layer.
  438.      *
  439.      * @param c  the Component to check
  440.      * @return an int giving the component's position, where 0 is the
  441.      *         topmost position and the highest index value = the count
  442.      *         count of components at that layer, minus 1
  443.      *
  444.      * @see #getComponentCountInLayer
  445.      */
  446.     public int getPosition(Component c) {
  447.         int i, count, startLayer, curLayer, startLocation, pos = 0;
  448.         
  449.         count = getComponentCount();
  450.         startLocation = getIndexOf(c);
  451.  
  452.         if(startLocation == -1)
  453.             return -1;
  454.         
  455.         startLayer = getLayer(c);
  456.         for(i = startLocation - 1; i >= 0; i--) {
  457.             curLayer = getLayer(getComponent(i));               
  458.             if(curLayer == startLayer)
  459.                 pos++;
  460.             else
  461.                 return pos;
  462.         }
  463.         return pos;
  464.     }
  465.  
  466.     /** Returns the highest layer value from all current children.
  467.       * Returns 0 if there are no children.
  468.       *
  469.       * @return an int indicating the layer of the topmost component in the 
  470.       *         pane, or zero if there are no children
  471.       */
  472.     public int highestLayer() {
  473.         if(getComponentCount() > 0)
  474.             return getLayer(getComponent(0));           
  475.         return 0;
  476.     }
  477.  
  478.     /** Returns the lowest layer value from all current children.
  479.       * Returns 0 if there are no children.
  480.       *
  481.       * @return an int indicating the layer of the bottommost component in the 
  482.       *         pane, or zero if there are no children
  483.       */
  484.     public int lowestLayer() {
  485.         int count = getComponentCount();
  486.         if(count > 0)
  487.             return getLayer(getComponent(count-1));             
  488.         return 0;
  489.     }
  490.  
  491.     /**
  492.      * Returns the number of children currently in the specified layer.
  493.      *
  494.      * @param layer  an int specifying the layer to check
  495.      * @return an int specifying the number of components in that layer
  496.      */
  497.     public int getComponentCountInLayer(int layer) {
  498.         int i, count, curLayer;
  499.         int layerCount = 0;
  500.         
  501.         count = getComponentCount();
  502.         for(i = 0; i < count; i++) {
  503.             curLayer = getLayer(getComponent(i));               
  504.             if(curLayer == layer) {
  505.                 layerCount++;
  506.             /// Short circut the counting when we have them all
  507.             } else if(layerCount > 0 || curLayer < layer) {
  508.                 break;
  509.             }
  510.         }
  511.         
  512.         return layerCount;
  513.     }
  514.  
  515.     /**
  516.      * Returns an array of the components in the specified layer.
  517.      *
  518.      * @param layer  an int specifying the layer to check
  519.      * @return an array of Components contained in that layer
  520.      */
  521.     public Component[] getComponentsInLayer(int layer) {
  522.         int i, count, curLayer;
  523.         int layerCount = 0;
  524.         Component[] results;
  525.         
  526.         results = new Component[getComponentCountInLayer(layer)];
  527.         count = getComponentCount();
  528.         for(i = 0; i < count; i++) {
  529.             curLayer = getLayer(getComponent(i));               
  530.             if(curLayer == layer) {
  531.                 results[layerCount++] = getComponent(i);
  532.             /// Short circut the counting when we have them all
  533.             } else if(layerCount > 0 || curLayer < layer) {
  534.                 break;
  535.             }
  536.         }
  537.         
  538.         return results;
  539.     }
  540.  
  541.     /**
  542.      * Paints this JLayeredPane within the specified graphics context.
  543.      *
  544.      * @param g  the Graphics context within which to paint
  545.      */
  546.     public void paint(Graphics g) {
  547.         if(isOpaque()) {
  548.             Rectangle r = g.getClipBounds();
  549.             Color c = getBackground();
  550.             if(c == null)
  551.                 c = Color.lightGray;
  552.             g.setColor(c);
  553.             g.fillRect(r.x, r.y, r.width, r.height);
  554.         }
  555.         super.paint(g);
  556.     }
  557.  
  558. //////////////////////////////////////////////////////////////////////////////
  559. //// Implementation Details
  560. //////////////////////////////////////////////////////////////////////////////
  561.  
  562.     /**
  563.      * Returns the hashtable that maps components to layers.
  564.      *
  565.      * @return the Hashtable used to map components to their layers
  566.      */
  567.     protected Hashtable getComponentToLayer() {
  568.         if(componentToLayer == null)
  569.             componentToLayer = new Hashtable(4);
  570.         return componentToLayer;
  571.     }
  572.     
  573.     /**
  574.      * Returns the Integer object associated with a specified layer.
  575.      *
  576.      * @param layer an int specifying the layer
  577.      * @return an Integer object for that layer
  578.      */
  579.     protected Integer getObjectForLayer(int layer) {
  580.         Integer layerObj;
  581.         switch(layer) {
  582.         case 0:
  583.             layerObj = DEFAULT_LAYER;
  584.             break;
  585.         case 100:
  586.             layerObj = PALETTE_LAYER;
  587.             break;
  588.         case 200:
  589.             layerObj = MODAL_LAYER;
  590.             break;
  591.         case 300:
  592.             layerObj = POPUP_LAYER;
  593.             break;
  594.         case 400:
  595.             layerObj = DRAG_LAYER;
  596.             break;
  597.         default:
  598.             layerObj = new Integer(layer);
  599.         }
  600.         return layerObj;
  601.     }
  602.  
  603.     /** 
  604.      * Primative method that determines the proper location to
  605.      * insert a new child based on layer and position requests.
  606.      * 
  607.      * @param layer     an int specifying the layer
  608.      * @param position  an int specifying the position within the layer
  609.      * @return an int giving the (absolute) insertion-index
  610.      *
  611.      * @see #getIndexOf
  612.      */
  613.     protected int insertIndexForLayer(int layer, int position) {
  614.         int i, count, curLayer;
  615.         int layerStart = -1;
  616.         int layerEnd = -1;
  617.         
  618.         count = getComponentCount();
  619.         for(i = 0; i < count; i++) {
  620.             curLayer = getLayer(getComponent(i));               
  621.             if(layerStart == -1 && curLayer == layer) {
  622.                 layerStart = i;
  623.             }   
  624.             if(curLayer < layer ) {
  625.                 if(i == 0) { 
  626.                     // layer is greater than any current layer  
  627.                     // [ ASSERT(layer > highestLayer()) ] 
  628.                     layerStart = 0;
  629.                     layerEnd = 0;
  630.                 } else {
  631.                     layerEnd = i;
  632.                 }
  633.                 break;
  634.             }
  635.         }
  636.  
  637.         // layer requested is lower than any current layer
  638.         // [ ASSERT(layer < lowestLayer()) ] 
  639.         // put it on the bottom of the stack
  640.         if(layerStart == -1 && layerEnd == -1)
  641.             return count;
  642.  
  643.         // In the case of a single layer entry handle the degenerative cases
  644.         if(layerStart != -1 && layerEnd == -1)
  645.             layerEnd = count;
  646.         
  647.         if(layerEnd != -1 && layerStart == -1)
  648.             layerStart = layerEnd;
  649.         
  650.         // If we are adding to the bottom, return the last element
  651.         if(position == -1)
  652.             return layerEnd;
  653.         
  654.         // Otherwise make sure the requested position falls in the 
  655.         // proper range
  656.         if(position > -1 && layerStart + position <= layerEnd)
  657.             return layerStart + position;
  658.         
  659.         // Otherwise return the end of the layer
  660.         return layerEnd;
  661.     }
  662.  
  663. /////////////////
  664. // Accessibility support
  665. ////////////////
  666.  
  667.     /**
  668.      * Get the AccessibleContext associated with this JComponent
  669.      *
  670.      * @return the AccessibleContext of this JComponent
  671.      */
  672.     public AccessibleContext getAccessibleContext() {
  673.         if (accessibleContext == null) {
  674.             accessibleContext = new AccessibleJLayeredPane();
  675.         }
  676.         return accessibleContext;
  677.     }
  678.  
  679.     /**
  680.      * The class used to obtain the accessible role for this object.
  681.      * <p>
  682.      * Warning: serialized objects of this class will not be compatible with
  683.      * future swing releases.  The current serialization support is appropriate
  684.      * for short term storage or RMI between Swing1.0 applications.  It will
  685.      * not be possible to load serialized Swing1.0 objects with future releases
  686.      * of Swing.  The JDK1.2 release of Swing will be the compatibility
  687.      * baseline for the serialized form of Swing objects.
  688.      */
  689.     protected class AccessibleJLayeredPane extends AccessibleJComponent {
  690.  
  691.         /**
  692.          * Get the role of this object.
  693.          *
  694.          * @return an instance of AccessibleRole describing the role of the 
  695.          * object
  696.          * @see AccessibleRole
  697.          */
  698.         public AccessibleRole getAccessibleRole() {
  699.             return AccessibleRole.LAYERED_PANE;
  700.         }
  701.     }
  702. }
  703.  
  704.  
  705.